var ns, app, dialog, toast;
var app = new Vue({
    el: '#app',
    data: {
        // IsAccept:同意,IsReject:拒绝,IsRevoke:撤销,
        list: [],
        IsReject:0,//---1-表示拒绝 0表示同意
        ApprovalID:0,
        PendingUserID:0,//---待审批用户UserID
        AcceptCont:'',//---内容描述
        ischecked:false,
        chooseType:1,
        choose_id_list:[],
        select_id_list:[],
    },
    methods: {
        checkbox:function(){
            var that=this;
            that.ischecked=event.target.checked;
            // if(that.ischecked==true){
            //     that.do_openperson();
            // }
        },
        do_openperson:function(){
            ns.openWin('chooseperson_frm', '选择', {
                source:'chooseuser',
            });
        },
        do_save:function(){
            var that=this;
            var options={};
            options.ApprovalID = that.ApprovalID;
            if(that.IsReject==0){//同意
                options.Type=1;
            }
             if(that.IsReject==1){//拒绝
                options.Type=2;
            }

            options.action = "getisoperate";
            ns.post(options, function (succeed, data, err) {
                if(data.status==true){
                    that.do_saveinto();
                }
                else{
                    api.toast({
                        msg: '状态已改变，请重新选择',
                        duration: 2000,
                        location: 'bottom'
                    });
                    setTimeout(function () {
                        api.closeWin();
                    }, 500);
                }
            });

        },
        //展开选择的全部人员
        open_more:function(type){
            var that=this;
            //审批全部
            if(type==1){
                ns.openWin('examine_Allperson', '全部审核人员', {
                    list: that.choose_id_list,
                    type:type,
                 }); 
            }else if(type==0){
                ns.openWin('examine_Allperson', '全部抄送人员', {
                    list: that.select_id_list,
                    type:type,
                 }); 
            }

        },
        //选择人
        choose_persons: function(IsApproval) {
            var that = this;
            that.chooseType=IsApproval;
            // IsApproval==0//抄送
            if(IsApproval==0){
                // ns.openWin('chooseperson_frm', '请选择', {
                //         source: 'transfer',
                // }); 
                ns.openWin('chooseuser_frm', '请选择抄送人', {
                    source: 'transfer',
                }); 
            }
            //IsApproval==1//审批
            if(IsApproval==1){
                // ns.openWin('chooseperson_frm', '请选择', {
                //     source: 'transfer',
                // }); 
                ns.openWin('chooseuser_frm', '请选择审批人', {
                    source: 'transfer',
                });
            }
               
        },
        //移除抄送人
        guanbi_pic:function(id,IsApproval){
            var that=this;
            that.chooseType=IsApproval;
            if(that.chooseType==1){
                // that.choose_id_list.splice(index,1);
                that.choose_id_list = that.choose_id_list.filter((item) => {
                    return item.UserID != id
                    });
            }
            if(that.chooseType==0){
                // console.log(JSON.stringify(that.select_id_list));
                // that.select_id_list.splice(index,1);
                that.select_id_list = that.select_id_list.filter((item) => {
                    return item.UserID != id
                    });
            }
            
        },
        // 抄送人
        select_person:function(select_list,type){
            var that=this;
            if(type==0){
                for (var i = 0; i < select_list.length; i++) {
                    var item = select_list[i];
                    that.select_id_list.push(item);
                }
            }else{
                for (var i = 0; i < select_list.length; i++) {
                    var item = select_list[i];
                    that.choose_id_list.push(item);
                }
            }
            
            let obj = {};
            if(type==0){
                let peon = that.select_id_list.reduce((cur,next) => {
                    obj[next.UserID] ? "" : obj[next.UserID] = true && cur.push(next);
                    return cur;
                },[]) ;
                that.select_id_list=peon;
            }else{
                let peon = that.choose_id_list.reduce((cur,next) => {
                    obj[next.UserID] ? "" : obj[next.UserID] = true && cur.push(next);
                    return cur;
                },[]) ;
                that.choose_id_list=peon;
            }
           
        },

        do_saveinto:function(){
            var that=this;
            var PendingUserIDList=[];
            var SendUserIDList=[];
            if(that.ischecked){
                if(that.choose_id_list.length<=0){
                    api.toast({
                        msg: '请选择审批人',
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }else{
                    for (let i = 0; i < that.choose_id_list.length; i++) {
                        PendingUserIDList.push(that.choose_id_list[i].UserID);
                    }
                }
            }
            if(that.select_id_list.length>0){
                for (let i = 0; i < that.select_id_list.length; i++) {
                    SendUserIDList.push(that.select_id_list[i].UserID);
                }
            }

            var options={};
            options.IsReject=that.IsReject;
            options.ApprovalID=that.ApprovalID;
            options.PendingUserIDList=JSON.stringify(PendingUserIDList);
            options.SendUserIDList=JSON.stringify(SendUserIDList);
            options.AcceptCont=that.AcceptCont;
            options.action="approvalacceptnew";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.AcceptCont='';
                    api.toast({
                        msg: '提交成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    setTimeout(function () {
                        api.closeWin();
                    }, 500);
                    api.sendEvent({
                        name: 'reloadexaminelist',
                    });
                    api.sendEvent({
                        name: 'reloadexaminedetail',
                    });
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });

        },
        // guanbi_pic:function(index,IsApproval){
        //     var that=this;
        //     that.chooseType=IsApproval;
        //     if(that.chooseType==1){
        //         that.choose_id_list.splice(index,1);
        //     };
        //     that.ischecked=false;
        // },
        //移除抄送人
        guanbi_pic:function(id,IsApproval){
            var that=this;
            that.chooseType=IsApproval;
            if(that.chooseType==1){
                that.choose_id_list = that.choose_id_list.filter((item) => {
                    return item.UserID != id
                    });
            }
            if(that.chooseType==0){
                that.select_id_list = that.select_id_list.filter((item) => {
                    return item.UserID != id
                    });
            }
            
        },
       




    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    dialog = new auiDialog();
    toast = new auiToast();
    app.ApprovalID = api.pageParam.ApprovalID ;
    app.IsReject = api.pageParam.IsReject ;
    
    // ns.init_pull();
    api.addEventListener({
        name: 'purchase_add'
    }, function (ret) {
        if( app.chooseType==1){
            var chooselist=ret.value.data;
            app.select_person(chooselist,1);
            // app.choose_id_list=ret.value.data;
        }
        if( app.chooseType==0){

            var select_list=ret.value.data;
            app.select_person(select_list,0);
        }

    })
    api.addEventListener({
        name: 'purchase_update'
    }, function (ret) {
        if( ret.value.type==1){
            app.choose_id_list=ret.value.data;
        }
        if( ret.value.type==0){
            app.select_id_list=ret.value.data;
        }

    })
    
};
